package siwi.map.android;

import com.actionbarsherlock.app.SherlockActivity;
import com.wmw.Animation.ViewAction;
import com.wmw.Network.NetworkStateMonitor;
import com.wmw.Utility.Common;
import com.wmw.Utility.MSG;
import com.wmw.Utility.RegularExpression;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnticipateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterSecondStep extends SherlockActivity implements
		OnClickListener {
	// 每一步,用字写上一些帮助信息
	private InputBox _restaurantName, _phoneNumber, _address2;

	private EditText view_userName;
	private EditText view_password;
	private EditText view_passwordConfirm;
	private Button view_submit;
	private Button view_clearAll;
	private static final int MENU_EXIT = Menu.FIRST - 1;
	private static final int MENU_ABOUT = Menu.FIRST;

	private Intent _gpsService;
	private GPSAddrReceiverForRSS _receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_second_step);

		final EditText etRestaurantName = (EditText) findViewById(R.id.RSS_RestaurantName);
		ImageView clearRestaurantName = (ImageView) findViewById(R.id.RSS_ClearRestaurantName);
		_restaurantName = new InputBox(this, etRestaurantName,
				clearRestaurantName, null, etRestaurantName.getPaddingRight(),
				RegularExpression.RestaurantName, new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etRestaurantName
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etRestaurantName
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		final EditText etPhoneNumber = (EditText) findViewById(R.id.RSS_PhoneNumber);
		ImageView clearPhoneNumber = (ImageView) findViewById(R.id.RSS_ClearPhoneNumber);
		_phoneNumber = new InputBox(this, etPhoneNumber, clearPhoneNumber,
				null, etPhoneNumber.getPaddingRight(),
				RegularExpression.PhoneNumber, new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etPhoneNumber
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etPhoneNumber
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		final EditText etAddressMoreDetail = (EditText) findViewById(R.id.RSS_Address2);
		ImageView clearAddressMoreDetail = (ImageView) findViewById(R.id.RSS_ClearAddress2);
		_address2 = new InputBox(this, etAddressMoreDetail,
				clearAddressMoreDetail, null,
				etAddressMoreDetail.getPaddingRight(),
				RegularExpression.Address, new InputBox.ShakeBox() {
					@Override
					public void Action(EditText et, String regularExpression,
							Context context, View v, boolean hasFocus) {
						super.Action(et, regularExpression, context, v,
								hasFocus);
					}

					@Override
					public void OnFocusChange(boolean valide, boolean hasFocus) {
						super.OnFocusChange(valide, hasFocus);
						Log.d(Common.GetLineInfo(), "Focus -" + hasFocus);
						if (hasFocus)
							etAddressMoreDetail
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.TextBackground));
						else if (valide)
							etAddressMoreDetail
									.setBackgroundColor(RegisterSecondStep.this
											.getResources().getColor(
													R.color.Transparent));
					}
				});

		Button button = (Button) findViewById(R.id.RSS_NextStep);
		button.setOnClickListener(this);

		_gpsService = new Intent(RegisterSecondStep.this,
				siwi.map.android.GPSLocateService.class);
		startService(_gpsService);

		if (!NetworkStateMonitor.isNetworkAvailable(RegisterSecondStep.this)) {
			Log.d(Common.GetLineInfo(), "Doesn't open network");
			Spanned info = Html.fromHtml("<font color='#FF0000'>" + "当前网络连接不可用"
					+ "</FONT>");
			((TextView) findViewById(R.id.RSS_Address)).setText(info);
		}

		findViewById(R.id.LocateAddress).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Toast.makeText(RegisterSecondStep.this, "正在定位中",
								Toast.LENGTH_SHORT).show();
					}
				});
	}

	@Override
	protected void onStart() {
		super.onStart();
		_receiver = new GPSAddrReceiverForRSS();
		// 创建过滤器，并指定action，使之用于接收同action的广播
		IntentFilter filter = new IntentFilter("MyReceiver_Action");
		// 注册广播接收器
		registerReceiver(_receiver, filter);
	}

	@Override
	protected void onStop() {
		super.onStop();

		// 注销广播接收器
		unregisterReceiver(_receiver);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		Log.d(Common.GetLineInfo(), "Stopping GPS Service");
		stopService(_gpsService);
	}

	public void SetGPSAddr(String addr) {
		TextView et = (TextView) findViewById(R.id.RSS_Address);

		int lastIdx = addr.length() - 1;
		for (; lastIdx >= 0; --lastIdx) {
			char c = addr.charAt(lastIdx);
			String str = String.valueOf(c);
			if (!str.matches(RegularExpression.LettersAndNumbers))
				break;
		}
		++lastIdx;
		if (lastIdx < addr.length()) {
			Log.d(this.toString(), "substring - " + addr.substring(lastIdx));
			addr = addr.replaceAll(addr.substring(lastIdx), "");
		}
		Log.d(Common.MARS, "substring - " + Common.GetLineInfo());
		et.setText(addr);
	}

	@Override
	public void onClick(View v) {
		ViewAction.startSpringMenuAnimations(v);

		if (!_restaurantName.isValide()) {
			Toast.makeText(RegisterSecondStep.this, "请输入正确的店铺名",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (!_phoneNumber.isValide()) {
			Toast.makeText(RegisterSecondStep.this, "请输入正确的手机号码",
					Toast.LENGTH_SHORT).show();
			return;
		}

		if (!_address2.isValide()) {
			Toast.makeText(RegisterSecondStep.this, "请输入正确的详细地址",
					Toast.LENGTH_SHORT).show();
			return;
		}

		// TODO
		Intent intent = new Intent();
		intent.setClass(RegisterSecondStep.this, RegisterThirdStep.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
		startActivity(intent);
	}

	// findViews();
	// setListener();
	// 然后执行注册的事情,访问远程服务器注册用户
}
//
// /** 1.初始化注册view组件 */
// private void findViews() {
// view_userName = (EditText) findViewById(R.id.registerUserName);
// view_password = (EditText) findViewById(R.id.registerPassword);
// view_passwordConfirm = (EditText) findViewById(R.id.registerPasswordConfirm);
// view_submit = (Button) findViewById(R.id.registerSubmit);
// view_clearAll = (Button) findViewById(R.id.registerClear);
// }
//
// private void setListener() {
// view_submit.setOnClickListener(submitListener);
// view_clearAll.setOnClickListener(clearListener);
// }
//
// /** 监听注册确定按钮 */
// private OnClickListener submitListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// String userName = view_userName.getText().toString();
// String password = view_password.getText().toString();
// String passwordConfirm = view_passwordConfirm.getText().toString();
// validateForm(userName, password, passwordConfirm);
// }
// };
//
// /** 清空监听按钮 */
// private OnClickListener clearListener = new OnClickListener() {
// @Override
// public void onClick(View v) {
// clearForm();
// }
// };
//
// /** 检查注册表单 */
// private void validateForm(String userName, String password, String password2)
// {
// StringBuilder suggest = new StringBuilder();
// Log.d(this.toString(), "validate");
// if (userName.length() < 1) {
// suggest.append(getText(R.string.suggust_userName) + "\n");
// }
// if (password.length() < 1 || password2.length() < 1) {
// suggest.append(getText(R.string.suggust_passwordNotEmpty) + "\n");
// }
// if (!password.equals(password2)) {
// suggest.append(getText(R.string.suggest_passwordNotSame));
// }
// if (suggest.length() > 0) {
// //sub是因为要出去最后一个 \n换行符
// Toast.makeText(this, suggest.subSequence(0, suggest.length() - 1),
// Toast.LENGTH_SHORT).show();
// }
// }
//
// /** 清空表单 */
// private void clearForm() {
// view_userName.setText("");
// view_password.setText("");
// view_passwordConfirm.setText("");
//
// view_userName.requestFocus();
// }
//
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// super.onCreateOptionsMenu(menu);
// menu.add(0, MENU_EXIT, 0, R.string.MENU_EXIT);
// menu.add(0, MENU_ABOUT, 0, R.string.MENU_ABOUT);
// return true;
// }
//
// @Override
// public boolean onMenuItemSelected(int featureId, MenuItem item) {
// super.onMenuItemSelected(featureId, item);
// switch (item.getItemId()) {
// case MENU_EXIT:
// finish();
// break;
// case MENU_ABOUT:
// alertAbout();
// break;
// }
// ;
// return true;
// }
//
// /** 弹出关于对话框 */
// private void alertAbout() {
// new AlertDialog.Builder(Register.this).setTitle(R.string.MENU_ABOUT)
// .setMessage(R.string.aboutInfo).setPositiveButton(
// R.string.ok_label,
// new DialogInterface.OnClickListener() {
// public void onClick(
// DialogInterface dialoginterface, int i) {
// }
// }).show();
// }
// }
